package org.fitweb.examples;

import org.fitweb.fixture.WebServerProvider;

@SuppressWarnings("unused")
public class ExampleWebServerProvider implements WebServerProvider {

    private static MinimalWebServer server;

    @Override
    public boolean start(int port) {
        return findOrStartOn(port) != null;
    }

    @Override public boolean stop() {
        server.stop();
        server = null;
        return true;
    }

    private static MinimalWebServer findOrStartOn(int port) {
        if (server == null) {
            server = new MinimalWebServer(port);
        } else if (server.port != port) {
            throw new IllegalStateException("You already have a server listening on " + server.port + ", but you are trying to start one on " + port);
        }

        return server;
    }

    public static void main(String[] args) {
        findOrStartOn(8008);
    }
}
